package com.achievo.know.application.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.achievo.know.application.TestApplication;
import com.achievo.know.domain.TestEntity;
import com.achievo.know.domain.TestRespository;
import com.achievo.know.vo.TestVO;

@Service
@Transactional
public class TestApplicationImpl implements TestApplication {
	
	@Autowired
	private TestRespository respository;
	
	public void setRespository(TestRespository respository) {
		this.respository = respository;
	}

	@Override
	public TestVO add(TestVO vo) {
		TestEntity test=new TestEntity(vo);
		test.save();
		return new TestVO(test.getName(),test.getId());
	}

	@Override
	public void delete(String[] ids) {
		for(String id:ids){
			TestEntity test=TestEntity.get(Long.parseLong(id));
			test.remove();
		}
	}

	@Override
	public TestVO edit(TestVO vo) {
		TestEntity test=TestEntity.get(vo.getId());
		test.update(vo);
		return new TestVO(test.getName(),test.getId());
	}

	@Override
	public TestVO get(Long id) {
		TestEntity test= TestEntity.get(id);
		return new TestVO(test.getName(),test.getId());
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<TestVO> listAll() {
		return respository.find("select new com.achievo.know.vo.TestVO(t.name,t.id) from TestEntity t",new Object[]{});
	}

}
